//
//  ZYOilsPriceViewController.m
//  ZYOils
//
//  Created by 段新瑞 on 2019/9/30.
//  Copyright © 2019 Mr.Xr. All rights reserved.
//

#import "ZYOilsPriceViewController.h"
#import "ZYMyStoreOilsPriceTableViewCell.h"
#import "ZYOilsAddTableViewCell.h"
#import "ZYChangeOilsPriceViewController.h"
#import "XRRefreshHeader.h"
#import "XRRefreshFooter.h"
#import "ZYOilsInfoModel.h"

@interface ZYOilsPriceViewController () <SWTableViewCellDelegate, UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *oilsLists;

@property (nonatomic) NSInteger page;

@end

@implementation ZYOilsPriceViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configViews];
    [self configData];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

#pragma mark - Setup View / Data

- (void)configViews {
    self.title = @"油价";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYOilsAddTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZYOilsAddTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYMyStoreOilsPriceTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZYMyStoreOilsPriceTableViewCell"];
    @weakify(self)
    self.tableView.mj_header = [XRRefreshHeader headerWithRefreshingBlock:^{
        @strongify(self)
        [self getNewData];
    }];
    
    self.tableView.mj_footer = [XRRefreshFooter footerWithRefreshingBlock:^{
        @strongify(self)
        [self getMoreData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer.hidden = YES;
}

- (void)getNewData {
    NSDictionary *dict = @{@"companyId": ZYUserInfoModel.sharedInfoModel.userModel.companyId,
                           @"page": [NSString stringWithFormat:@"%ld", self.page],
                           @"size": @"100"};
    // [ZYHud showLoadingText:@""];
    [[DataManager manager] postFindOilParam:dict block:^(XRCommonModel *model, NSError *error) {
        // [ZYHud hideHud];
        [self.tableView.mj_header endRefreshing];
        if (model.isSuccess) {
            // self.tableView.mj_footer.hidden = NO;
            self.page = 1;
            self.oilsLists = [ZYOilsInfoModel mj_objectArrayWithKeyValuesArray:model.data];
            [self.tableView reloadData];
            /**
            if (self.oilsLists.count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableView.mj_footer resetNoMoreData];
            }
             */
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
        }
    }];
}

- (void)getMoreData {
    NSDictionary *dict = @{@"companyId": ZYUserInfoModel.sharedInfoModel.userModel.companyId,
                           @"page": [NSString stringWithFormat:@"%ld", self.page],
                           @"size": @"10"};
    [ZYHud showLoadingText:@""];
    [[DataManager manager] postFindOilParam:dict block:^(XRCommonModel *model, NSError *error) {
        [ZYHud hideHud];
        [self.tableView.mj_footer endRefreshing];
        if (model.isSuccess) {
            self.page += 1;
            [self.oilsLists addObjectsFromArray:[ZYOilsInfoModel mj_objectArrayWithKeyValuesArray:model.data]];
            if ([ZYOilsInfoModel mj_objectArrayWithKeyValuesArray:model.data].count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableView.mj_footer resetNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
        }
    }];
}

- (void)configData {
    self.page = 1;
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return self.oilsLists.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ZYOilsAddTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYOilsAddTableViewCell"];
        return cell;
    } else {
        ZYMyStoreOilsPriceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYMyStoreOilsPriceTableViewCell"];
        cell.rightUtilityButtons = [self rightButtons];
        cell.delegate = self;
        cell.model = self.oilsLists[indexPath.row];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    @weakify(self)
    if (indexPath.section == 0) {
        ZYChangeOilsPriceViewController *controller = [ZYChangeOilsPriceViewController new];
        controller.title = @"新增油品";
        controller.state = OilsStateAdd;
        [controller.subject subscribeNext:^(id  _Nullable x) {
            @strongify(self)
            [self getNewData];
        }];
        [self.navigationController pushViewController:controller animated:YES];
    } else {
        ZYChangeOilsPriceViewController *controller = [ZYChangeOilsPriceViewController new];
        controller.title = @"修改油品";
        controller.model = self.oilsLists[indexPath.row];
        controller.state = OilsStateChange;
        [controller.subject subscribeNext:^(id  _Nullable x) {
            @strongify(self)
            [self.tableView reloadData];
        }];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

#pragma mark - Observer

#pragma mark - Notification

#pragma mark - Event Response

#pragma mark - Override Methods

#pragma mark - Delegate
#pragma mark - SWTableViewDelegate
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index {
    switch (index) {
        case 0: {
            [ZYHud showLoadingText:@""];
            NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
            ZYOilsInfoModel *model = self.oilsLists[cellIndexPath.row];
            NSDictionary *dict = @{@"oilId": model.oilId};
            [[DataManager manager] postDeleteOilParam:dict block:^(XRCommonModel *model, NSError *error) {
                [ZYHud hideHud];
                if (model.isSuccess) {
                    [self.oilsLists removeObjectAtIndex:cellIndexPath.row];
                    [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath]
                            withRowAnimation:UITableViewRowAnimationLeft];
                } else {
                    if (![LBLimitInput isEmpty:model.error]) {
                        [ZYHud showErrorText:model.error];
                    }
                }
            }];
            break;
        }
        default:
            break;
    }
}

- (NSArray *)rightButtons {
    NSMutableArray *rightUtilityButtons = [NSMutableArray new];
    [rightUtilityButtons sw_addUtilityButtonWithColor:COLORHEX(@"d72b2c")
                                                title:@"删除"];
    return rightUtilityButtons;
}

- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell {
    
    // allow just one cell's utility button to be open at once
    return YES;
}

- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state {
    
    switch (state) {
        case 1:
            // set to NO to disable all left utility buttons appearing
            return NO;
            break;
        case 2:
            // set to NO to disable all right utility buttons appearing
            return YES;
            break;
        default:
            break;
    }
    
    return YES;
}

#pragma mark - Public Methods

#pragma mark - Private Methods

#pragma mark - Setter / Getter

- (NSMutableArray *)oilsLists {
    if (!_oilsLists) {
        _oilsLists = [[NSMutableArray alloc] init];
    }
    return _oilsLists;
}


#pragma mark - Network

    
    
    



@end
